package server

import (
	"io"
	"os"

	"github.com/gin-gonic/gin"

	"gitee.com/diandisoft/ant_forest_energy_web_server/config"
	"gitee.com/diandisoft/ant_forest_energy_web_server/controllers"
	"gitee.com/diandisoft/ant_forest_energy_web_server/middlewares"
)

func Init() {
	config := config.GetConfig()
	r := NewRouter()
	r.Run(config.GetString("server.port"))
}

func NewRouter() *gin.Engine {
	router := gin.New()

	gin.DisableConsoleColor()
	f, _ := os.Create("gin.log")
	gin.DefaultWriter = io.MultiWriter(f, os.Stdout)

	router.Use(gin.Logger())
	router.Use(gin.Recovery())

	// 检测服务运行状态
	health := new(controllers.HealthController)
	router.GET("/health", health.Status)

	// 中间件
	//router.Use(middlewares.AuthMiddleware)
	//	router.Use(middlewares.DbConnect)
	router.Use(middlewares.ErrorHandler)

	// API 路由
	v1 := router.Group("collent")
	{
		collentGroup := v1.Group(":user")
		{
			collent := new(controllers.CollentController)
			collentGroup.POST("add", collent.CollentRecords)
			collentGroup.GET("lasttime", collent.LastCollentTime)
		}
	}
	v2 := router.Group("withcollent")
	{
		collentGroup := v2.Group(":user")
		{
			collent := new(controllers.CollentController)
			collentGroup.GET("withcollent", collent.WithCollentRecords)
		}
	}

	return router

}
